package jsyf.oa.service.device;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.device.BrandStrockDao;
import jsyf.oa.entity.device.BrandStrockEntity;
import jsyf.oa.facade.device.BrandStrockFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class BrandStrockService implements BrandStrockFacade {

    @Autowired
    private BrandStrockDao brandStrockDao;

    @Override
    public Boolean save(String jsonData) {
        List<BrandStrockEntity> brandStrockEntityList = JSON.parseArray(jsonData, BrandStrockEntity.class);
        //删除全部
        brandStrockDao.removeByCondition("", new String[]{});
        for (int i = 0; i < brandStrockEntityList.size(); i++) {
            BrandStrockEntity brandStrockEntity = brandStrockEntityList.get(i);
            brandStrockEntity.setSortNo(i);
            brandStrockEntity.setType(0);//0-提货地址
            brandStrockDao.add(brandStrockEntity);
        }
        return true;
    }

    @Override
    public List<JSONObject> selectByBrandId(String jsonData) {
        JSONObject jsonObject = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String brandId = jsonObject.getString("brandId");
        String type = jsonObject.getString("type");//区分是否需要下拉框去重
        return brandStrockDao.selectByBrandId(brandId,type);
    }

    @Override
    public List<JSONObject> getContactsByAddress(String jsonData) {
        JSONObject jsonObject = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String address = jsonObject.getString("address");
        return brandStrockDao.getContactsByAddress(address);
    }

    @Override
    public JSONObject getTelByContactByAddress(String jsonData) {
        JSONObject object = JSON.parseObject(jsonData);
        String address=object.getString("address");
        String contact=object.getString("contact");
        StringBuffer condition=new StringBuffer();
        condition.append(" AND BRAND_ADDRESS=? AND CONTACT=?");
        return brandStrockDao.getTelByContactByAddress(condition.toString(),new Object[]{address,contact});
    }

    @Override
    public List<JSONObject> getTransportCompany(String jsonData) {
        JSONObject object = JSON.parseObject(jsonData);
        String key = object.getString("key");
        List<JSONObject> transports=brandStrockDao.getTransportMessage();
        /*if(StringUtils.isBlank(key)){
          return transports;
        }
        List<JSONObject> search=new ArrayList<JSONObject>();
        for(int i=0;i<transports.size();i++){
            if(transports.get(i).getString("companyName").contains(key)){
                search.add(transports.get(i));
            }
        }*/
        return transports;
    }
}
